package com.example.lanouhn.firstapp.activity.fragment;

import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.adapter.CommentAdapter;
import com.example.lanouhn.firstapp.activity.model.Comment;
import com.example.lanouhn.firstapp.activity.utils.CommentJson;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;

import java.util.List;


public class IntoGoodsCommentFragmen extends Fragment {
    //传递id
    private String goods_id;
    private List<Comment> list;
    private CommentAdapter adapter;
    //加载进度
    private ProgressBar progressBar;
    private RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_into_goods_comment_fragmen,null);

        goods_id = getActivity().getIntent().getStringExtra("str");
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

        LinearLayoutManager linear = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linear);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getDataFromWeb();

    }

    /**
     * 开启线程
     */
    private void getDataFromWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //调用HttpUtils类,来请求网络获取Json数据
                String result = HttpUtils.doGet(getRequestUrl());
                //将获取到的Json数据解析,通过封装的工具类
                list = CommentJson.getComment(result);
                //判断解析出的集合长度,有数据就显示,无数据就不显示
                if (list.size() > 0)
                    handler.sendEmptyMessage(1);
                else
                    handler.sendEmptyMessage(0);
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //取消loading  进度条
//            progressBar.setVisibility(View.GONE);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(),
                            "请求失败,请稍候再试！", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    //获取数据成功,并解析数据成功,创建adapter
                    adapter = new CommentAdapter( getActivity(),list);
                    //设置adapter
                    recyclerView.setAdapter(adapter);
                    break;
            }
        }
    };


    //http://www.niaolema.com/mobile/index.php?act=buycomments&op=comments_list&goods_id=49
    /**
     * http://www.niaolema.com/mobile/index.php?act=buycomments&op=comments_list&goods_id=49
     * 拼接网络地址  并将其返回
     * @return
     */
    private String getRequestUrl() {
        return "http://www.niaolema.com/mobile/index.php?act=buycomments&op=comments_list&goods_id=49" ;
    }
}
